package com.lian.anlimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lian.anlimall.ware.entity.WareInfoEntity;
import com.lian.anlimall.ware.feign.ProductFeignService;
import com.lian.anlimall.ware.service.WareInfoService;
import com.lian.common.utils.PageUtils;
import com.lian.common.utils.Query;
import com.lian.anlimall.ware.dao.PurchaseDetailDao;
import com.lian.anlimall.ware.entity.PurchaseDetailEntity;
import com.lian.anlimall.ware.service.PurchaseDetailService;
import com.lian.common.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Autowired
    private ProductFeignService productFeignService;
    @Autowired
    private WareInfoService wareInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<PurchaseDetailEntity>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(wrapper -> {
                wrapper.eq("purchase_id",key).or().eq("sku_id",key);
            });
        }

        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)) {
            queryWrapper.eq("status",status);
        }

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId) && !"0".equalsIgnoreCase(wareId)) {
            queryWrapper.eq("ware_id",wareId);
        }

        queryWrapper.orderByAsc("status");
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );
        List<PurchaseDetailEntity> purchaseDetailEntities = page.getRecords().stream().map(item-> {
            R skuInfo = productFeignService.info(item.getSkuId());
            Map<String,Object> data = (Map<String, Object>) skuInfo.get("skuInfo");
            item.setSkuName((String) data.get("skuName"));
            item.setSkuImg((String) data.get("skuDefaultImg"));

            WareInfoEntity  wareInfo = wareInfoService.getById(item.getWareId());
            item.setWareName(wareInfo.getName());
            return item;
        }).collect(Collectors.toList());

        page.setRecords(purchaseDetailEntities);

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {

        List<PurchaseDetailEntity> purchaseId = this.list(
                new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));

        return purchaseId;
    }

    @Override
    public PageUtils queryPageByPurchaseId(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<PurchaseDetailEntity>();

        String purchaseId = (String) params.get("purchaseId");
        if (!StringUtils.isEmpty(purchaseId)) {
            queryWrapper.and(wrapper -> {
                wrapper.eq("purchase_id",purchaseId);
            });
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );
        List<PurchaseDetailEntity> purchaseDetailEntities = page.getRecords().stream().map(item-> {
            R skuInfo = productFeignService.info(item.getSkuId());
            Map<String,Object> data = (Map<String, Object>) skuInfo.get("skuInfo");
            item.setSkuName((String) data.get("skuName"));
            item.setSkuImg((String) data.get("skuDefaultImg"));

            WareInfoEntity  wareInfo = wareInfoService.getById(item.getWareId());
            item.setWareName(wareInfo.getName());
            return item;
        }).collect(Collectors.toList());

        page.setRecords(purchaseDetailEntities);

        return new PageUtils(page);
    }

    @Override
    public void updateskuPrice(Long id, BigDecimal skuPrice) {

        PurchaseDetailEntity purchaseDetailEntity = this.baseMapper.selectById(id);
        purchaseDetailEntity.setSkuPrice(skuPrice);
        this.baseMapper.updateById(purchaseDetailEntity);
    }

}